<!DOCTYPE html>
<html lang="zh" data-direction="ltr">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta
      name="keywords"
      content="Go-Micro, Micro, 中文, Go-Micro教程, Go-Micro资源"
    />
    <meta
      name="description"
      content="Micro 中国站，只为原生中文资源"
    />
    <title>机器人 - Micro 中国</title>
    <meta data-rh="true" property="og:type" content="website"/><meta data-rh="true" property="og:image" content=""/><meta data-rh="true" property="og:title" content="机器人 - Micro 中国"/><meta data-rh="true" name="description" content="micro bot是一只藏在微服务中的小马蝇，有了它，我们可以在 Slack、HipChat、XMPP 等等聊天程序中与它对话，通过它来操控服务。"/>
    <link
      rel="icon"
      href="https://avatars2.githubusercontent.com/u/20906580?s=60&v=4"
      type="image/x-icon"
    />
    
    <link rel="stylesheet" type="text/css" href="/index-47dfeecd.css" />
    
    <style id="nprogress-style">
      #nprogress {
        display: none;
      }
    </style>
    <link rel="stylesheet/less" type="text/css" href="/color.less" />
    <script src="https://polyfill.alicdn.com/polyfill.min.js?features=default,es2015,Intl"></script>
    <script>
      (function () {
        function isLocalStorageNameSupported() {
          var testKey = 'test';
          var storage = window.localStorage;
          try {
            storage.setItem(testKey, '1');
            storage.removeItem(testKey);
            return true;
          } catch (error) {
            return false;
          }
        }
        // 优先级提高到所有静态资源的前面，语言不对，加载其他静态资源没意义
        var pathname = location.pathname;

        function isZhCN(pathname) {
          return /-cn\/?$/.test(pathname);
        }
        function getLocalizedPathname(path, zhCN) {
          var pathname = path.startsWith('/') ? path : '/' + path;
          if (!zhCN) {
            // to enUS
            return /\/?index-cn/.test(pathname) ? '/' : pathname.replace('-cn', '');
          } else if (pathname === '/') {
            return '/index-cn';
          } else if (pathname.endsWith('/')) {
            return pathname.replace(/\/$/, '-cn/');
          }
          return pathname + '-cn';
        }

        // 兼容旧的 URL， `?locale=...`
        var queryString = location.search;
        if (queryString) {
          var isZhCNConfig = queryString.indexOf('zh-CN') > -1;
          if (isZhCNConfig && !isZhCN(pathname)) {
            location.pathname = getLocalizedPathname(pathname, isZhCNConfig);
          }
        }

        // 首页无视链接里面的语言设置 https://github.com/ant-design/ant-design/issues/4552
        if (isLocalStorageNameSupported() && (pathname === '/' || pathname === '/index-cn')) {
          var lang =
            (window.localStorage && localStorage.getItem('locale')) ||
            ((navigator.language || navigator.browserLanguage).toLowerCase() === 'zh-cn'
              ? 'zh-CN'
              : 'en-US');
          // safari is 'zh-cn', while other browser is 'zh-CN';
          if ((lang === 'zh-CN') !== isZhCN(pathname)) {
            location.pathname = getLocalizedPathname(pathname, lang === 'zh-CN');
          }
        }
        document.documentElement.className += isZhCN(pathname) ? 'zh-cn' : 'en-us';
      })();
    </script>
  </head>
  <body>
    <div id="react-content">
      <div class="page-wrapper"><header id="header" class="clearfix"><div class="ant-row" style="flex-flow:nowrap;height:64px"><div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-6 ant-col-xl-5 ant-col-xxl-4"><h1><a id="logo" href="/index-cn"><img alt="logo" src=""/>Micro 中国</a></h1></div><div class="ant-col menu-row ant-col-xs-0 ant-col-sm-0 ant-col-md-18 ant-col-lg-18 ant-col-xl-19 ant-col-xxl-20"><div id="search-box" class=""><span role="img" aria-label="search" class="anticon anticon-search"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="search" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M909.6 854.5L649.9 594.8C690.2 542.7 712 479 712 412c0-80.2-31.3-155.4-87.9-212.1-56.6-56.7-132-87.9-212.1-87.9s-155.5 31.3-212.1 87.9C143.2 256.5 112 331.8 112 412c0 80.1 31.3 155.5 87.9 212.1C256.5 680.8 331.8 712 412 712c67 0 130.6-21.8 182.7-62l259.7 259.6a8.2 8.2 0 0011.6 0l43.6-43.5a8.2 8.2 0 000-11.6zM570.4 570.4C528 612.7 471.8 636 412 636s-116-23.3-158.4-65.6C211.3 528 188 471.8 188 412s23.3-116.1 65.6-158.4C296 211.3 352.2 188 412 188s116.1 23.2 158.4 65.6S636 352.2 636 412s-23.3 116.1-65.6 158.4z"></path></svg></span><input type="text" placeholder="在 Micro China 中搜索" class="ant-input" value=""/></div><ul class="ant-menu menu-site ant-menu-light ant-menu-root ant-menu-horizontal" id="nav" direction="ltr" role="menu"><li class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none" role="menuitem"><div class="ant-menu-submenu-title" role="button" aria-expanded="false" aria-haspopup="true"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem"><a href="/docs/micro/introduce-cn">文档</a></li><li class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none" role="menuitem"><div class="ant-menu-submenu-title" role="button" aria-expanded="false" aria-haspopup="true"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem"><a href="/components/button-cn/">插件</a></li><li class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none" role="menuitem"><div class="ant-menu-submenu-title" role="button" aria-expanded="false" aria-haspopup="true"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem"><a href="/docs/resources-cn">资源</a></li><li class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="visibility:hidden;position:absolute" role="menuitem"><div class="ant-menu-submenu-title" role="button" aria-expanded="false" aria-haspopup="true"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li></ul><button type="button" class="ant-btn header-button header-lang-button ant-btn-sm">繁体中文</button><button type="button" class="ant-btn header-button ant-dropdown-trigger ant-btn-sm">更多<span role="img" aria-label="down" style="font-size:9px;margin-left:2px;vertical-align:middle;margin-top:-1px" class="anticon anticon-down"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="down" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M884 256h-75c-5.1 0-9.9 2.5-12.9 6.6L512 654.2 227.9 262.6c-3-4.1-7.8-6.6-12.9-6.6h-75c-6.5 0-10.3 7.4-6.5 12.7l352.6 486.1c12.8 17.6 39 17.6 51.7 0l352.6-486.1c3.9-5.3.1-12.7-6.4-12.7z"></path></svg></span></button><span id="github-btn" class="github-btn"><a class="gh-btn" href="//github.com/micro/go-micro/" target="_blank"><span class="gh-ico" aria-hidden="true"></span><span class="gh-text">Star</span></a><a class="gh-count" target="_blank" href="//github.com/micro/go-micro/stargazers/"></a></span></div></div></header><div class="main-wrapper"><div class="ant-row"><div class="ant-col main-menu ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-6 ant-col-xl-5 ant-col-xxl-4"><div><div class=""><section class="main-menu-inner"><ul class="ant-menu aside-container menu-site ant-menu-light ant-menu-root ant-menu-inline" direction="ltr" role="menu"><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/getting-starter-cn">快速上手</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/api-cn">API</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/architecture-cn">架构</a></li><li class="ant-menu-item ant-menu-item-only-child ant-menu-item-selected" role="menuitem" style="padding-left:30px"><a href="/docs/micro/bot-cn">机器人</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/cli-cn">CLI</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/debugging-cn">Debug</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/deploy-docker-cn">使用Docker发布</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/faq-cn">常见问题</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/deploy-kubernetes-cn">kubernetes</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/fault-tolerance-cn">容错</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/features-cn">特性</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/go-api-cn">Go API</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/go-config-cn">Go Config 配置库</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/go-grpc-cn">Go gRPC支持</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/go-micro-cn">Go Micro</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/go-micro-internals-cn">Go Micro 架构</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/grpc-gateway-cn">GRPC 网关</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro-cn">Micro 文档</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/install-guide-cn">安装指导</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/writing-a-go-function-cn">编写Golang Function</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/writing-a-go-service-cn">编写Golang服务</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/introduce-cn">Go-Micro 框架</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/plugins-cn">插件库</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/plugins-go-micro-cn">Go Micro插件</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/proxy-cn">Proxy</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/runtime-cn">Micro Runtime</a></li><li class="ant-menu-item ant-menu-item-only-child" role="menuitem" style="padding-left:30px"><a href="/docs/micro/resources-cn">相关资源</a></li></ul></section></div></div></div><div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-18 ant-col-lg-18 ant-col-xl-19 ant-col-xxl-20"><section class="main-container"><article class="markdown"><div><h1>机器人<a class="edit-button" href="https://github.com/ant-design/ant-design/edit/master/docs/micro/bot.zh-CN.md" target="_blank" rel="noopener noreferrer"><span role="img" aria-label="edit" class="anticon anticon-edit"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="edit" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M257.7 752c2 0 4-.2 6-.5L431.9 722c2-.4 3.9-1.3 5.3-2.8l423.9-423.9a9.96 9.96 0 000-14.1L694.9 114.9c-1.9-1.9-4.4-2.9-7.1-2.9s-5.2 1-7.1 2.9L256.8 538.8c-1.5 1.5-2.4 3.3-2.8 5.3l-29.5 168.2a33.5 33.5 0 009.4 29.8c6.6 6.4 14.9 9.9 23.8 9.9zm67.4-174.4L687.8 215l73.3 73.3-362.7 362.6-88.9 15.7 15.6-89zM880 836H144c-17.7 0-32 14.3-32 32v36c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-36c0-17.7-14.3-32-32-32z"></path></svg></span></a></h1></div><div class="toc-affix"><div class=""><ul class="toc"><li><a class="bisheng-toc-h1" href="#micro-bot" title="micro bot">micro bot</a></li><li><a class="bisheng-toc-h2" href="#现在支持的输入方式" title="现在支持的输入方式">现在支持的输入方式</a></li><li><a class="bisheng-toc-h2" href="#准备上车" title="准备上车">准备上车</a></li><li><a class="bisheng-toc-h2" href="#增加命令" title="增加命令">增加命令</a></li><li><a class="bisheng-toc-h2" href="#增加新的输入源" title="增加新的输入源">增加新的输入源</a></li><li><a class="bisheng-toc-h2" href="#命令即服务" title="命令即服务">命令即服务</a></li></ul></div></div><section class="markdown"><h1 id="micro-bot"><span>micro bot</span><a href="#micro-bot" class="anchor">#</a></h1><p><strong>micro bot</strong>是一只藏在微服务中的小马蝇，有了它，我们可以在 Slack、HipChat、XMPP 等等聊天程序中与它对话，通过它来操控服务。</p><p>我们把消息发送给它，它基于这些消息模仿执行 CLI，触发指定的接口功能。</p><p align="center"><img src="../images/bot.png"/></p><h2 id="现在支持的输入方式"><span>现在支持的输入方式</span><a href="#现在支持的输入方式" class="anchor">#</a></h2><ul><li><p>Slack</p></li><li><p>HipChat</p></li></ul><h2 id="准备上车"><span>准备上车</span><a href="#准备上车" class="anchor">#</a></h2><h3 id="安装-Micro"><span>安装 Micro</span><a href="#安装-Micro" class="anchor">#</a></h3><p>安装 Micro，已经安装跳过</p><pre class="language-go"><code><span class="token keyword">go</span> get github<span class="token punctuation">.</span>com<span class="token operator">/</span>micro<span class="token operator">/</span>micro</code></pre><h3 id="slack"><span>slack</span><a href="#slack" class="anchor">#</a></h3><p>SLACK_TOKEN 需要到<a href="https://api.slack.com/tokens">Slack</a>上去获取。</p><pre class="language-shell"><code>micro bot <span class="token operator">-</span><span class="token operator">-</span>inputs<span class="token operator">=</span>slack <span class="token operator">-</span><span class="token operator">-</span>slack_token<span class="token operator">=</span>SLACK_TOKEN</code></pre><p>效果见图：</p><p><img src="../images/slack.png"/></p><p>-</p><h3 id="HipChat"><span>HipChat</span><a href="#HipChat" class="anchor">#</a></h3><pre class="language-shell"><code>micro bot <span class="token operator">-</span><span class="token operator">-</span>inputs<span class="token operator">=</span>hipchat <span class="token operator">-</span><span class="token operator">-</span>hipchat_username<span class="token operator">=</span>XMPP_USER <span class="token operator">-</span><span class="token operator">-</span>hipchat_password<span class="token operator">=</span>XMPP_PASSWORD</code></pre><p>效果见图：</p><p><img src="../images/hipchat.png"/></p><p>-</p><p>如果使用多种机器人，可以使用逗号分隔传入：</p><pre class="language-shell"><code>micro bot <span class="token operator">-</span><span class="token operator">-</span>inputs<span class="token operator">=</span>hipchat<span class="token punctuation">,</span>slack <span class="token operator">-</span><span class="token operator">-</span>slack_token<span class="token operator">=</span>SLACK_TOKEN <span class="token operator">-</span><span class="token operator">-</span>hipchat_username<span class="token operator">=</span>XMPP_USER <span class="token operator">-</span><span class="token operator">-</span>hipchat_password<span class="token operator">=</span>XMPP_PASSWORD</code></pre><h3 id="帮助信息"><span>帮助信息</span><a href="#帮助信息" class="anchor">#</a></h3><p>Slack</p><pre class="language-shell"><code>micro help

deregister service <span class="token punctuation">[</span>definition<span class="token punctuation">]</span> <span class="token operator">-</span> Deregisters a service
echo <span class="token punctuation">[</span>text<span class="token punctuation">]</span> <span class="token operator">-</span> Returns the <span class="token punctuation">[</span>text<span class="token punctuation">]</span>
get service <span class="token punctuation">[</span>name<span class="token punctuation">]</span> <span class="token operator">-</span> Returns a registered service
health <span class="token punctuation">[</span>service<span class="token punctuation">]</span> <span class="token operator">-</span> Returns health of a service
hello <span class="token operator">-</span> Returns a greeting
list services <span class="token operator">-</span> Returns a list of registered services
ping <span class="token operator">-</span> Returns pong
query <span class="token punctuation">[</span>service<span class="token punctuation">]</span> <span class="token punctuation">[</span>method<span class="token punctuation">]</span> <span class="token punctuation">[</span>request<span class="token punctuation">]</span> <span class="token operator">-</span> Returns the response <span class="token keyword">for</span> a service query
register service <span class="token punctuation">[</span>definition<span class="token punctuation">]</span> <span class="token operator">-</span> Registers a service
the three laws <span class="token operator">-</span> Returns the three laws of robotics
time <span class="token operator">-</span> Returns the server time</code></pre><h2 id="增加命令"><span>增加命令</span><a href="#增加命令" class="anchor">#</a></h2><p>小机器人是根据文本消息匹配命令执行指定功能。</p><h3 id="编写命令"><span>编写命令</span><a href="#编写命令" class="anchor">#</a></h3><pre class="language-go"><code><span class="token keyword">import</span> <span class="token string">"github.com/micro/go-micro/v2/agent/command"</span>

<span class="token keyword">func</span> <span class="token function">Ping</span><span class="token punctuation">(</span><span class="token punctuation">)</span> command<span class="token punctuation">.</span>Command <span class="token punctuation">{</span>
	usage <span class="token operator">:=</span> <span class="token string">"ping"</span>
	description <span class="token operator">:=</span> <span class="token string">"Returns pong"</span>

	<span class="token keyword">return</span> command<span class="token punctuation">.</span><span class="token function">NewCommand</span><span class="token punctuation">(</span><span class="token string">"ping"</span><span class="token punctuation">,</span> usage<span class="token punctuation">,</span> desc<span class="token punctuation">,</span> <span class="token keyword">func</span><span class="token punctuation">(</span>args <span class="token operator">...</span><span class="token builtin">string</span><span class="token punctuation">)</span> <span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token builtin">byte</span><span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span><span class="token string">"pong"</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token boolean">nil</span>
	<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span></code></pre><h3 id="注册命令"><span>注册命令</span><a href="#注册命令" class="anchor">#</a></h3><p>把命令加到命令映射表中，命令的 key 可以通过<strong>golang/regexp.Match</strong>正则匹配。</p><pre class="language-go"><code><span class="token keyword">import</span> <span class="token string">"github.com/micro/go-micro/v2/agent/command"</span>

<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	command<span class="token punctuation">.</span>Commands<span class="token punctuation">[</span><span class="token string">"^ping$"</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token function">Ping</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span></code></pre><h3 id="重构建-Micro-服务"><span>重构建 Micro 服务</span><a href="#重构建-Micro-服务" class="anchor">#</a></h3><p>打包二进制文件</p><pre class="language-shell"><code>cd github<span class="token punctuation">.</span>com<span class="token operator">/</span>micro<span class="token operator">/</span>micro

<span class="token operator">/</span><span class="token operator">/</span> 本地打包
go build <span class="token operator">-</span>i <span class="token operator">-</span>o micro <span class="token punctuation">.</span><span class="token operator">/</span>main<span class="token punctuation">.</span>go

<span class="token operator">/</span><span class="token operator">/</span> docker方式打包
CGO_ENABLED<span class="token operator">=</span><span class="token number">0</span> GOOS<span class="token operator">=</span>linux go build <span class="token operator">-</span>a <span class="token operator">-</span>installsuffix cgo <span class="token operator">-</span>ldflags <span class="token string">'-w'</span> <span class="token operator">-</span>i <span class="token operator">-</span>o micro <span class="token punctuation">.</span><span class="token operator">/</span>main<span class="token punctuation">.</span>go</code></pre><h2 id="增加新的输入源"><span>增加新的输入源</span><a href="#增加新的输入源" class="anchor">#</a></h2><p>输入针对通信软件是插件化的，比如 Slack、HipChat、XMPP、IRC、SMTP 等等。</p><p>新的输入源可以通过下列方式增加。</p><h3 id="新增输入源"><span>新增输入源</span><a href="#新增输入源" class="anchor">#</a></h3><p>输入源要满足下面的接口。</p><pre class="language-go"><code><span class="token keyword">type</span> Input <span class="token keyword">interface</span> <span class="token punctuation">{</span>
	<span class="token comment" spellcheck="true">// 命令行接口标识</span>
	<span class="token function">Flags</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>cli<span class="token punctuation">.</span>Flag
	<span class="token comment" spellcheck="true">// 使用命令行上下文初始化</span>
	<span class="token function">Init</span><span class="token punctuation">(</span><span class="token operator">*</span>cli<span class="token punctuation">.</span>Context<span class="token punctuation">)</span> <span class="token builtin">error</span>
	<span class="token comment" spellcheck="true">// 输入流事件</span>
	<span class="token function">Stream</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">(</span>Conn<span class="token punctuation">,</span> <span class="token builtin">error</span><span class="token punctuation">)</span>
	<span class="token comment" spellcheck="true">// 开始输入</span>
	<span class="token function">Start</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">error</span>
	<span class="token comment" spellcheck="true">// 输入终止</span>
	<span class="token function">Stop</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">error</span>
	<span class="token comment" spellcheck="true">// 输入源名</span>
	<span class="token function">String</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token builtin">string</span>
<span class="token punctuation">}</span></code></pre><h3 id="注册输入"><span>注册输入</span><a href="#注册输入" class="anchor">#</a></h3><p>把新的输入注册到映射表中</p><pre class="language-go"><code><span class="token keyword">import</span> <span class="token string">"github.com/micro/go-micro/v2/agent/input"</span>

<span class="token keyword">func</span> <span class="token function">init</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	input<span class="token punctuation">.</span>Inputs<span class="token punctuation">[</span><span class="token string">"name"</span><span class="token punctuation">]</span> <span class="token operator">=</span> MyInput
<span class="token punctuation">}</span></code></pre><h3 id="重构建"><span>重构建</span><a href="#重构建" class="anchor">#</a></h3><p>二进制打包</p><pre class="language-shell"><code>cd github<span class="token punctuation">.</span>com<span class="token operator">/</span>micro<span class="token operator">/</span>micro

<span class="token operator">/</span><span class="token operator">/</span> 本地打包
go build <span class="token operator">-</span>i <span class="token operator">-</span>o micro <span class="token punctuation">.</span><span class="token operator">/</span>main<span class="token punctuation">.</span>go

<span class="token operator">/</span><span class="token operator">/</span> docker打包
CGO_ENABLED<span class="token operator">=</span><span class="token number">0</span> GOOS<span class="token operator">=</span>linux go build <span class="token operator">-</span>a <span class="token operator">-</span>installsuffix cgo <span class="token operator">-</span>ldflags <span class="token string">'-w'</span> <span class="token operator">-</span>i <span class="token operator">-</span>o micro <span class="token punctuation">.</span><span class="token operator">/</span>main<span class="token punctuation">.</span>go</code></pre><h2 id="命令即服务"><span>命令即服务</span><a href="#命令即服务" class="anchor">#</a></h2><p>小机器人支持创建服务式的命令。</p><h3 id="是怎么工作的"><span>是怎么工作的</span><a href="#是怎么工作的" class="anchor">#</a></h3><p>小机器人值班时，它会监测它命名空间中注册的服务，默认的命名空间是<code>go.micro.bot</code>。所有在这个命名空间内的服务都会自动加到可执行列表中，当命令执行后，小机器人就会通过方法<code>Command.Exec</code>来调用服务。它也支持使用<code>Command.Help</code> 获取使用信息，当然是注册时要把帮助信息写上。</p><p>服务的接口示例如下，也可以在<a href="https://github.com/micro/go-micro/agent/blob/master/proto/bot.proto">go-bot/proto</a>上找到。</p><pre class="language-proto"><code>syntax <span class="token operator">=</span> <span class="token string">"proto3"</span><span class="token comment" spellcheck="true">;</span>

package go<span class="token punctuation">.</span>micro<span class="token punctuation">.</span>bot<span class="token comment" spellcheck="true">;</span>

service Command {
	rpc <span class="token function">Help</span><span class="token punctuation">(</span>HelpRequest<span class="token punctuation">)</span> returns <span class="token punctuation">(</span>HelpResponse<span class="token punctuation">)</span> {}<span class="token comment" spellcheck="true">;</span>
	rpc <span class="token function">Exec</span><span class="token punctuation">(</span>ExecRequest<span class="token punctuation">)</span> returns <span class="token punctuation">(</span>ExecResponse<span class="token punctuation">)</span> {}<span class="token comment" spellcheck="true">;</span>
}

message HelpRequest {
}

message HelpResponse {
	string usage <span class="token operator">=</span> <span class="token number">1</span><span class="token comment" spellcheck="true">;</span>
	string description <span class="token operator">=</span> <span class="token number">2</span><span class="token comment" spellcheck="true">;</span>
}

message ExecRequest {
	repeated string args <span class="token operator">=</span> <span class="token number">1</span><span class="token comment" spellcheck="true">;</span>
}

message ExecResponse {
	bytes result <span class="token operator">=</span> <span class="token number">1</span><span class="token comment" spellcheck="true">;</span>
	string error <span class="token operator">=</span> <span class="token number">2</span><span class="token comment" spellcheck="true">;</span>
}</code></pre><h3 id="示例"><span>示例</span><a href="#示例" class="anchor">#</a></h3><p>这里有一个示例，以调用微服务来执行打印命令</p><pre class="language-go"><code><span class="token keyword">package</span> main

<span class="token keyword">import</span> <span class="token punctuation">(</span>
	<span class="token string">"fmt"</span>
	<span class="token string">"strings"</span>

	<span class="token string">"github.com/micro/go-micro/v2"</span>
	<span class="token string">"golang.org/x/net/context"</span>

	proto <span class="token string">"github.com/micro/go-micro/v2/agent/proto"</span>
<span class="token punctuation">)</span>

<span class="token keyword">type</span> Command <span class="token keyword">struct</span><span class="token punctuation">{</span><span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// Help 返回使用帮助信息</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>Command<span class="token punctuation">)</span> <span class="token function">Help</span><span class="token punctuation">(</span>ctx context<span class="token punctuation">.</span>Context<span class="token punctuation">,</span> req <span class="token operator">*</span>proto<span class="token punctuation">.</span>HelpRequest<span class="token punctuation">,</span> rsp <span class="token operator">*</span>proto<span class="token punctuation">.</span>HelpResponse<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
	<span class="token comment" spellcheck="true">// Usage 应该要包含命令的名称</span>
	rsp<span class="token punctuation">.</span>Usage <span class="token operator">=</span> <span class="token string">"echo"</span>
	rsp<span class="token punctuation">.</span>Description <span class="token operator">=</span> <span class="token string">"This is an example bot command as a micro service which echos the message"</span>
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token comment" spellcheck="true">// Exec 指行命令</span>
<span class="token keyword">func</span> <span class="token punctuation">(</span>c <span class="token operator">*</span>Command<span class="token punctuation">)</span> <span class="token function">Exec</span><span class="token punctuation">(</span>ctx context<span class="token punctuation">.</span>Context<span class="token punctuation">,</span> req <span class="token operator">*</span>proto<span class="token punctuation">.</span>ExecRequest<span class="token punctuation">,</span> rsp <span class="token operator">*</span>proto<span class="token punctuation">.</span>ExecResponse<span class="token punctuation">)</span> <span class="token builtin">error</span> <span class="token punctuation">{</span>
	rsp<span class="token punctuation">.</span>Result <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token function">byte</span><span class="token punctuation">(</span>strings<span class="token punctuation">.</span><span class="token function">Join</span><span class="token punctuation">(</span>req<span class="token punctuation">.</span>Args<span class="token punctuation">,</span> <span class="token string">" "</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token comment" spellcheck="true">// rsp.Error，如果发生错误，可以设置rsp.Error</span>
	<span class="token comment" spellcheck="true">// 注意：函数（Exec）返回的错误则只是服务这一级的错误信息，Exec调用方法会收到这个错误，而rsp.Error是返</span>
	<span class="token comment" spellcheck="true">// 回给命令执行处的错误</span>
	<span class="token keyword">return</span> <span class="token boolean">nil</span>
<span class="token punctuation">}</span>

<span class="token keyword">func</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
	service <span class="token operator">:=</span> micro<span class="token punctuation">.</span><span class="token function">NewService</span><span class="token punctuation">(</span>
		micro<span class="token punctuation">.</span><span class="token function">Name</span><span class="token punctuation">(</span><span class="token string">"go.micro.bot.echo"</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
	<span class="token punctuation">)</span>

	service<span class="token punctuation">.</span><span class="token function">Init</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

	proto<span class="token punctuation">.</span><span class="token function">RegisterCommandHandler</span><span class="token punctuation">(</span>service<span class="token punctuation">.</span><span class="token function">Server</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">new</span><span class="token punctuation">(</span>Command<span class="token punctuation">)</span><span class="token punctuation">)</span>

	<span class="token keyword">if</span> err <span class="token operator">:=</span> service<span class="token punctuation">.</span><span class="token function">Run</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> err <span class="token operator">!=</span> <span class="token boolean">nil</span> <span class="token punctuation">{</span>
		fmt<span class="token punctuation">.</span><span class="token function">Println</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre></section><section class="markdown api-container"></section></article><div class="contributors-list" style="display:flex;list-style:none;margin:0;padding:0"><span style="opacity:0.3" class="ant-avatar ant-avatar-circle"><span class="ant-avatar-string" style="opacity:0"></span></span></div></section><section class="prev-next-nav"><a class="prev-page" href="/docs/micro/architecture-cn"><span role="img" aria-label="left" class="anticon anticon-left footer-nav-icon-before"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 000 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></span>架构<span role="img" aria-label="right" class="anticon anticon-right footer-nav-icon-after"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 00302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 000-50.4z"></path></svg></span></a><a class="next-page" href="/docs/micro/cli-cn"><span role="img" aria-label="left" class="anticon anticon-left footer-nav-icon-before"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 000 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></span>CLI<span role="img" aria-label="right" class="anticon anticon-right footer-nav-icon-after"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 00302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 000-50.4z"></path></svg></span></a></section><footer class="rc-footer rc-footer-dark"><section class="rc-footer-container"><section class="rc-footer-columns"><div class="rc-footer-column"><h2>相关资源</h2><div class="rc-footer-item"><a href="https://github.com/micro-in-cn/platform-web" target="_blank">Platform Web 治理平台</a></div><div class="rc-footer-item"><a href="https://github.com/micro-in-cn/XConf" target="_blank">XConf 配置中心</a></div></div><div class="rc-footer-column"><h2>社区</h2><div class="rc-footer-item"><a href="https://github.com/websemantics/awesome-ant-design" target="_blank"><span class="rc-footer-item-icon"><span role="img" aria-label="comment" class="anticon anticon-comment"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="comment" width="1em" height="1em" fill="currentColor" aria-hidden="true"><defs><style></style></defs><path d="M573 421c-23.1 0-41 17.9-41 40s17.9 40 41 40c21.1 0 39-17.9 39-40s-17.9-40-39-40zm-280 0c-23.1 0-41 17.9-41 40s17.9 40 41 40c21.1 0 39-17.9 39-40s-17.9-40-39-40z"></path><path d="M894 345a343.92 343.92 0 00-189-130v.1c-17.1-19-36.4-36.5-58-52.1-163.7-119-393.5-82.7-513 81-96.3 133-92.2 311.9 6 439l.8 132.6c0 3.2.5 6.4 1.5 9.4a31.95 31.95 0 0040.1 20.9L309 806c33.5 11.9 68.1 18.7 102.5 20.6l-.5.4c89.1 64.9 205.9 84.4 313 49l127.1 41.4c3.2 1 6.5 1.6 9.9 1.6 17.7 0 32-14.3 32-32V753c88.1-119.6 90.4-284.9 1-408zM323 735l-12-5-99 31-1-104-8-9c-84.6-103.2-90.2-251.9-11-361 96.4-132.2 281.2-161.4 413-66 132.2 96.1 161.5 280.6 66 412-80.1 109.9-223.5 150.5-348 102zm505-17l-8 10 1 104-98-33-12 5c-56 20.8-115.7 22.5-171 7l-.2-.1A367.31 367.31 0 00729 676c76.4-105.3 88.8-237.6 44.4-350.4l.6.4c23 16.5 44.1 37.1 62 62 72.6 99.6 68.5 235.2-8 330z"></path><path d="M433 421c-23.1 0-41 17.9-41 40s17.9 40 41 40c21.1 0 39-17.9 39-40s-17.9-40-39-40z"></path></svg></span></span>讨论</a></div><div class="rc-footer-item"><a href="/docs/community/join-us-cn#加入我们"><span class="rc-footer-item-icon"><span role="img" aria-label="usergroup-add" class="anticon anticon-usergroup-add"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="usergroup-add" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M892 772h-80v-80c0-4.4-3.6-8-8-8h-48c-4.4 0-8 3.6-8 8v80h-80c-4.4 0-8 3.6-8 8v48c0 4.4 3.6 8 8 8h80v80c0 4.4 3.6 8 8 8h48c4.4 0 8-3.6 8-8v-80h80c4.4 0 8-3.6 8-8v-48c0-4.4-3.6-8-8-8zM373.5 498.4c-.9-8.7-1.4-17.5-1.4-26.4 0-15.9 1.5-31.4 4.3-46.5.7-3.6-1.2-7.3-4.5-8.8-13.6-6.1-26.1-14.5-36.9-25.1a127.54 127.54 0 01-38.7-95.4c.9-32.1 13.8-62.6 36.3-85.6 24.7-25.3 57.9-39.1 93.2-38.7 31.9.3 62.7 12.6 86 34.4 7.9 7.4 14.7 15.6 20.4 24.4 2 3.1 5.9 4.4 9.3 3.2 17.6-6.1 36.2-10.4 55.3-12.4 5.6-.6 8.8-6.6 6.3-11.6-32.5-64.3-98.9-108.7-175.7-109.9-110.8-1.7-203.2 89.2-203.2 200 0 62.8 28.9 118.8 74.2 155.5-31.8 14.7-61.1 35-86.5 60.4-54.8 54.7-85.8 126.9-87.8 204a8 8 0 008 8.2h56.1c4.3 0 7.9-3.4 8-7.7 1.9-58 25.4-112.3 66.7-153.5 29.4-29.4 65.4-49.8 104.7-59.7 3.8-1.1 6.4-4.8 5.9-8.8zM824 472c0-109.4-87.9-198.3-196.9-200C516.3 270.3 424 361.2 424 472c0 62.8 29 118.8 74.2 155.5a300.95 300.95 0 00-86.4 60.4C357 742.6 326 814.8 324 891.8a8 8 0 008 8.2h56c4.3 0 7.9-3.4 8-7.7 1.9-58 25.4-112.3 66.7-153.5C505.8 695.7 563 672 624 672c110.4 0 200-89.5 200-200zm-109.5 90.5C690.3 586.7 658.2 600 624 600s-66.3-13.3-90.5-37.5a127.26 127.26 0 01-37.5-91.8c.3-32.8 13.4-64.5 36.3-88 24-24.6 56.1-38.3 90.4-38.7 33.9-.3 66.8 12.9 91 36.6 24.8 24.3 38.4 56.8 38.4 91.4-.1 34.2-13.4 66.3-37.6 90.5z"></path></svg></span></span>加入我们</a></div></div><div class="rc-footer-column"><h2>帮助</h2><div class="rc-footer-item"><a href="https://github.com/micro-in-cn/questions" target="_blank"><span class="rc-footer-item-icon"><span role="img" aria-label="github" class="anticon anticon-github"><svg viewBox="64 64 896 896" focusable="false" class="" data-icon="github" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M511.6 76.3C264.3 76.2 64 276.4 64 523.5 64 718.9 189.3 885 363.8 946c23.5 5.9 19.9-10.8 19.9-22.2v-77.5c-135.7 15.9-141.2-73.9-150.3-88.9C215 726 171.5 718 184.5 703c30.9-15.9 62.4 4 98.9 57.9 26.4 39.1 77.9 32.5 104 26 5.7-23.5 17.9-44.5 34.7-60.8-140.6-25.2-199.2-111-199.2-213 0-49.5 16.3-95 48.3-131.7-20.4-60.5 1.9-112.3 4.9-120 58.1-5.2 118.5 41.6 123.2 45.3 33-8.9 70.7-13.6 112.9-13.6 42.4 0 80.2 4.9 113.5 13.9 11.3-8.6 67.3-48.8 121.3-43.9 2.9 7.7 24.7 58.3 5.5 118 32.4 36.8 48.9 82.7 48.9 132.3 0 102.2-59 188.1-200 212.9a127.5 127.5 0 0138.1 91v112.5c.8 9 0 17.9 15 17.9 177.1-59.7 304.6-227 304.6-424.1 0-247.2-200.4-447.3-447.5-447.3z"></path></svg></span></span>GitHub</a></div></div><div class="rc-footer-column"><h2><span class="rc-footer-column-icon"><img src="https://gw.alipayobjects.com/zos/rmsportal/nBVXkrFdWHxbZlmMbsaH.svg" alt="Ant XTech"/></span>更多产品</h2><div class="rc-footer-item"><a href="https://yuque.com" target="_blank"><span class="rc-footer-item-icon"><img src="https://gw.alipayobjects.com/zos/rmsportal/XuVpGqBFxXplzvLjJBZB.svg" alt="yuque"/></span>语雀</a><span class="rc-footer-item-separator">-</span><span class="rc-footer-item-description">知识创作与分享工具</span></div></div></section></section><section class="rc-footer-bottom"><div class="rc-footer-bottom-container">Made with <span style="color:#fff">❤</span> by<!-- --> <a target="_blank" rel="noopener noreferrer" href="https://xtech.antfin.com">XTech</a></div></section></footer></div></div></div></div>
    </div>
    
    <script src="/index-c81a5111.js"></script>
    
    <!-- Global Site Tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=UA-72788897-1"></script>
    <script>
      if (!location.port) {
        // Enable Google Analytics
        window.dataLayer = window.dataLayer || [];
        function gtag() {
          dataLayer.push(arguments);
        }
        gtag('js', new Date());
        gtag('config', 'UA-72788897-1');
      }
    </script>
    <!-- Hotjar Tracking Code for microhq.cn -->
    <script>
      (function (h, o, t, j, a, r) {
        h.hj =
          h.hj ||
          function () {
            (h.hj.q = h.hj.q || []).push(arguments);
          };
        h._hjSettings = { hjid: 1779539, hjsv: 6 };
        a = o.getElementsByTagName('head')[0];
        r = o.createElement('script');
        r.async = 1;
        r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv;
        a.appendChild(r);
      })(window, document, 'https://static.hotjar.com/c/hotjar-', '.js?sv=');
    </script>
  </body>
</html>
